/**
 *  API请求相关， 包括Token的管理维护
 */
function Request(tag) {

    this.tag = tag;
}

Request.prototype.TOKEN_TIME_KEY = "TOKEN_TIME_KEY";
Request.prototype.TOKEN_KEY = "TOKEN_KEY";
Request.prototype.TOKEN_LAST_USED = "TOKEN_LAST_USED";
Request.prototype.ENTERPRISE_ID = "ENTERPRISE_ID";

Request.prototype.host = 'http://39.104.96.79:8090';

Request.prototype.timestamp = function(){
	var now = new Date();
	return parseInt(now.getTime()/1000);
}

Request.prototype.setToken = function(token, lifetime, enterprise_id) {
    $api.setStorage(this.TOKEN_TIME_KEY, lifetime);
    $api.setStorage(this.TOKEN_KEY, token);
    $api.setStorage(this.TOKEN_LAST_USED, this.timestamp());
    $api.setStorage(this.ENTERPRISE_ID, enterprise_id);
}


Request.prototype.login = function(account, password, callback){

    var thiz = this;
    this.post('/user/sign/in', {
       account: account,
       password: password
    }, function(response, error){
      console.log(JSON.stringify(response));
        var result = false;
        if(response){

            if(0 == response.code){
                thiz.setToken(response.token, response.lifetime,response.enterprise);
                result = true;
            }
        }

        callback(result, error);
    });
}

Request.prototype.post = function(path, data, callback){

    data.token = $api.getStorage(this.TOKEN_KEY);
    var url = this.host + path;
    var thiz = this;
    api.ajax({
        url: url,
        tag: this.tag,
        method: 'post',
        data: {
            values: data
        }
    },function(response, error){

      if(response){
          if(-1 != response.code){
              thiz.setTokenLastUsed();
          }
          else{
              thiz.clearToken();
              api.closeToWin({
                  name: 'root'
              });
          }
      }

      callback(response, error);
    });
}

Request.prototype.cancle = function(){

    api.cancelAjax({
        tag: this.tag
    });

}

Request.prototype.setTokenLastUsed = function(){

    $api.setStorage(this.TOKEN_LAST_USED, this.timestamp());
}


Request.prototype.isLogin = function(){

    var result = false;

    var token = $api.getStorage(this.TOKEN_KEY);
    if(token){

         var lastUsed = $api.getStorage(this.TOKEN_LAST_USED);
         var tokenLiftime = $api.getStorage(this.TOKEN_TIME_KEY);

         if(this.timestamp() - parseInt(lastUsed) < tokenLiftime){

              result = true;
         }
    }

    return result;
}

Request.prototype.isEnterpriseUser = function(){

    var result = false;

    var id = $api.getStorage(this.ENTERPRISE_ID);
    if(id > 0){

        result = true;
    }

    return result;
}

Request.prototype.clearToken = function(){

    $api.rmStorage(this.TOKEN_TIME_KEY);
    $api.rmStorage(this.TOKEN_KEY);
    $api.rmStorage(this.TOKEN_LAST_USED);
    $api.rmStorage(this.ENTERPRISE_ID);

}

Request.prototype.searchFarmer = function(phone, callback){
    var thiz = this;
    this.post('/farmer/search', {
       phone: phone
    }, function(response, error){
      var response=JSON.stringify(response)
      console.log(response);
       sessionStorage["response"]=response;
      var result = false;
      if(0 == response.code){
        var result = true;
      }
        callback(response, error);
    });
}

Request.prototype.farmer = function(id, callback){
  var id=api.pageParam.id;
    var thiz = this;
    this.post('/farmer/information', {
       id:id
    }, function(response, error){
      console.log(JSON.stringify(response));
      var farmer=response.farmer
      var farmer=JSON.stringify(farmer);
      localStorage['farmer']=farmer
      var response=JSON.stringify(response)
      var result = false;
      if(0 == response.code){
        var result = true;
      }
        callback(response, error)
    })
}

Request.prototype.sheepRegistered = function(farmer, callback){
  var farmerId=api.pageParam.id;
    var thiz = this;
    this.post('/user/sheep/registered', {
       farmer:farmerId
    }, function(response, error){
        console.log(JSON.stringify(response));
        callback(response, error);
    });

}
Request.prototype.userinformation= function(callback){
    var thiz = this;
    this.post('/user/information', {},function(response, error){
      console.log(JSON.stringify(response));
      var response=JSON.stringify(response)
      var result = true;
      if(0 == response.code){
        var result = true;
      }
        callback(response, error);
    });
}
Request.prototype.signout= function(callback){
    var thiz = this;
    this.post('/user/sign/out', {},function(response, error){
      var result = false;
      if(0 == response.code){
        var result = true;
      }
        callback(result, error);
    });
}
Request.prototype.sheepExists= function(serial, callback){
    var thiz = this;
    this.post('/sheep/manage/exists', {
      number:serial
    },function(response, error){
      console.log(JSON.stringify(response));
      var result = false;
      if(true == response.exists){
        var result = true;
      }
        callback(result, error);
    });
}
Request.prototype.sheepRegister= function(farmer, number,latitude,longitude, callback){
    var thiz = this;
    this.post('/sheep/manage/register', {
      farmer:farmer,
      number:number,
      longitude:longitude,
      latitude:latitude
    },function(response, error){
      console.log(JSON.stringify(response));
      var result = false;
      if(0 == response.code){
        var result = true;
      }
      callback(result, error);
    });
 }
 Request.prototype.enterprise= function(callback){
     var thiz = this;
     this.post('/enterprise/information', {},function(response, error){
       var result = false;
       if(0 == response.code){
         var result = true;
       }
         callback(response, error);
     });
 }
 Request.prototype.enterpriseregister= function(number,latitude,longitude, callback){
     var thiz = this;
     this.post('/enterprise/sheep/register', {
       number:number,
       longitude:longitude,
       latitude:latitude
     },function(response, error){
       console.log(JSON.stringify(response));
       var result = false;
       if(0 == response.code){
         var result = true;
       }
         callback(response, error);
     });
 }
 Request.prototype.enterpriseregistered= function(callback){
     var thiz = this;
     this.post('/enterprise/sheep/registered', {},function(response, error){
       console.log(JSON.stringify(response));
       var result = false;
       if(0 == response.code){
         var result = true;
       }
         callback(response, error);
     });
 }
